package cn.lbwfree.web.repository.impl;

import cn.lbwfree.web.entity.Book;
import cn.lbwfree.web.entity.Borrow;
import cn.lbwfree.web.entity.Reader;
import cn.lbwfree.web.repository.BorrowRepository;
import cn.lbwfree.web.utils.JDBCTools;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BorrowRepositoryImpl implements BorrowRepository {
    @Override
    public void insert(Integer bookid, Integer readid, String borrowtime, String returntime, Integer adminid, Integer state) {
        Connection connection = JDBCTools.getConnection();
        String sql = "insert into borrow(bookid, readerid, borrowtime, returntime, state)value(?,?,?,?,?)";
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,bookid);
            preparedStatement.setInt(2, readid);
            preparedStatement.setString(3, borrowtime);
            preparedStatement.setString(4,returntime);
            preparedStatement.setInt(5, state);
            preparedStatement.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            JDBCTools.release(connection, preparedStatement, null);
        }

    }

    @Override
    public List<Borrow> findAllByReaderId(Integer id, Integer index, Integer limit) {
        Connection connection = JDBCTools.getConnection();
        String sql = "select br.id,b.name,b.author,b.publish,br.borrowtime,br.returntime,r.name,r.tel,r.cardid,br.state from borrow br,book b,reader r where readerid = ? and b.id = br.bookid and r.id = br.readerid limit ?,?";
        PreparedStatement statement =null;
        ResultSet resultSet = null;
        List<Borrow> list = new ArrayList<>();
        try {
            statement =connection.prepareStatement(sql);
            statement.setInt(1,id);
            statement.setInt(2, index);
            statement.setInt(3, limit);
            resultSet = statement.executeQuery();
            while(resultSet.next()){
                list.add(new Borrow(resultSet.getInt(1),
                        new Book(resultSet.getString(2), resultSet.getString(3), resultSet.getString(4)),
                        new Reader(resultSet.getString(7),resultSet.getString(8),resultSet.getString(9)),
                        resultSet.getString(5),
                        resultSet.getString(6),
                        resultSet.getInt(10)
                ));
            }

        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            JDBCTools.release(connection, statement, resultSet);
        }
        return list;
    }

    @Override
    public int count(Integer readerid) {
        Connection connection =JDBCTools.getConnection();
        String sql ="Select count(*) from borrow br,book b, reader r where readerid = ? and b.id=br.bookid and r.id=br.readerid";
        PreparedStatement preparedStatement =null;
        ResultSet resultSet=null;
        int count =0;
        try{
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setInt(1, readerid);
            resultSet = preparedStatement.executeQuery();
            if(resultSet.next()){
                count = resultSet.getInt(1);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            JDBCTools.release(connection, preparedStatement, resultSet);
        }
        return count;
    }

    @Override
    public List<Borrow> findAllByState(Integer state, Integer index, Integer limit) {
        Connection connection = JDBCTools.getConnection();
        String sql = "select br.id,b.name,b.author,b.publish,br.borrowtime,br.returntime,r.name,r.tel,r.cardid,br.state from borrow br,book b,reader r where state = ? and b.id = br.bookid and r.id = br.readerid limit ?,?";
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<Borrow> list = new ArrayList<>();
        try {
            statement = connection.prepareStatement(sql);
            statement.setInt(1,state);
            statement.setInt(2,index);
            statement.setInt(3,limit);
            resultSet = statement.executeQuery();
            //循环
            while(resultSet.next()){
                //取出所有的素材
                list.add(new Borrow(resultSet.getInt(1),
                        new Book(resultSet.getString(2),resultSet.getString(3),resultSet.getString(4)),
                        new Reader(resultSet.getString(7),resultSet.getString(8),resultSet.getString(9)),
                        resultSet.getString(5),
                        resultSet.getString(6),resultSet.getInt(10)));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCTools.release(connection,statement,resultSet);
        }
        return list;
    }

    @Override
    public int countByState(Integer state) {
        Connection connection =JDBCTools.getConnection();
        String sql ="select count(*) from borrow br,book b,reader r where state=? and br.bookid=b.id and r.id=br.readerid";
        PreparedStatement preparedStatement =null;
        ResultSet resultSet =null;
        int count =0;
        try {
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setInt(1, state);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()){
                count = resultSet.getInt(1);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            JDBCTools.release(connection ,preparedStatement, resultSet);
        }
        return count;
    }

    @Override
    public void handle(Integer borroeid, Integer state, Integer adminId) {
        Connection connection =JDBCTools.getConnection();
        String sql="update borrow set state=?,adminid=? where id =?";
        PreparedStatement preparedStatement =null;
        try{
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,state);
            preparedStatement.setInt(2,adminId);
            preparedStatement.setInt(3,borroeid);
            preparedStatement.executeUpdate();

        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            JDBCTools.release(connection, preparedStatement, null);
        }

    }
}
